function goOutsideUrl(url) { // 跳转外链
	// #ifdef  H5
	window.location.href = url;
	// #endif
	// #ifdef  APP-PLUS
	// uniapp打开浏览器
	plus.runtime.openURL(url);
	// #endif
}

// 系统信息
function mySysInfo() {
	const sys = uni.getSystemInfoSync();
	// console.log(sys);
	const data = {
		top: sys.statusBarHeight, // 手机状态栏的高度
		height: sys.statusBarHeight + uni.upx2px(90), // 手机状态栏的高度 + 90px
		screenHeight: sys.screenHeight,
		platform: sys.platform, // 操作系统
		model: sys.model,
		windowHeight: sys.windowHeight,
		windowTop: sys.windowTop,
		windowBottom: sys.windowBottom,
		deviceId: sys.deviceId,
		safeAreaInsets: sys.safeAreaInsets,
	};
	// #ifdef MP-WEIXIN || MP-BAIDU || MP-QQ || MP-TOUTIAO || MP-WEIXIN
	const custom = uni.getMenuButtonBoundingClientRect();
	data.top = custom.bottom;
	// #endif		
	// #ifdef MP-ALIPAY
	data.height = sys.statusBarHeight + sys.titleBarHeight;
	// #endif
	return data;
}

function getOSInformation() { // 得到操作系统信息
	return new Promise((resolve, reject) => {
		uni.getSystemInfo({
			success: (params) => {
				resolve(params); // params.osName === "ios"  params.osName === "android"等等
			},
			fail: (err) => {
				reject(err);
			}
		})
	})
}

function goBack(params) { // 返回上一页
	const pages = getCurrentPages()
	if (pages.length === 1) {
		if (typeof params == 'number') {
			history.go(-params)
		} else {
			history.back()
		}
	} else {
		uni.navigateBack({
			delta: 1
		});
	}
}

// 时间戳转换时分秒
function getHMS(timestamp) {
	let stamp = String(timestamp).length == 10 ? timestamp * 1000 : timestamp;
	let now = new Date().getTime();
	let times = stamp - now;
	let seconds = Math.floor(times / 1000);
	let minutes = Math.floor(seconds / 60);
	let hours = Math.floor(minutes / 60);
	minutes %= 60;
	seconds %= 60;
	let h = hours;
	let m = minutes.toString().padStart(2, '0');
	let s = seconds.toString().padStart(2, '0');
	return {
		h,
		m,
		s
	}
}

// 毫秒级时间戳转为 2022-1-18 10:09:06
function getDateFun(timestamp) {
	let stamp = String(timestamp).length == 10 ? timestamp * 1000 : timestamp;
	let now = new Date(stamp)
	let year = now.getFullYear();
	let month = now.getMonth() + 1;
	let date = now.getDate();
	let hour = now.getHours();
	let minute = now.getMinutes();
	let second = now.getSeconds();
	return year + "-" + formatNumber(month) + "-" + formatNumber(date) + " " + formatNumber(hour) + ":" + formatNumber(
		minute) + ":" + formatNumber(second);
}

function formatNumber(n) {
	n = n.toString()
	return n[1] ? n : '0' + n
}

// 跳转非tabbar页面
function navigateTo(url) {
	uni.navigateTo({
		url: url
	});
}

function scanCodeFun() {
	return new Promise((resolve, reject) => {
		uni.scanCode({
			success: function(res) {
				resolve(res)
			},
			fail: function(err) {
				reject(err)
			}
		})
	})

}

export default {
	goOutsideUrl,
	getOSInformation,
	mySysInfo,
	goBack,
	getHMS,
	getDateFun,
	formatNumber,
	navigateTo,
	scanCodeFun,
}